/*
 * @(#)TileManager.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.tile;

import javax.imageio.ImageIO;
import java.util.*;
import java.awt.image.*;
import java.awt.*;
import java.io.IOException;
import java.io.File;
import java.net.URL;

public class TileManager<KEY>
{
	protected ArrayList<TileSet<KEY>> tileSets;
	protected HashMap<KEY, BufferedImage> tiles;

	public TileManager()
	{
		tileSets = new ArrayList<TileSet<KEY>>();
		tiles = new HashMap<KEY,BufferedImage>();
	}

	public void addResource(KEY key, String resourceName)
	throws IOException
	{
		add(key, this.getClass().getResource(resourceName));
	}

	public void add(TileSet<KEY> tileSet)
	{
		tileSets.add(tileSet);
	}

	public void add(KEY key, BufferedImage tileImage)
	{
		tiles.put(key, tileImage);
	}

	public void add(KEY key, File tileImage)
	throws IOException
	{
		add(key, ImageIO.read(tileImage));
	}

	public void add(KEY key, URL tileImage)
	throws IOException
	{
		add(key, ImageIO.read(tileImage));
	}

	public boolean contains(KEY key)
	{
		if(tiles.containsKey(key))
			return true;

		for(TileSet<KEY> tileSet : tileSets)
		{
			if(tileSet.contains(key))
				return true;
		}

		return false;
	}

	public BufferedImage getTile(KEY key)
	{
		BufferedImage tileImage = tiles.get(key);
		if(tileImage != null)
			return tileImage;

		for(TileSet<KEY> tileSet : tileSets)
		{
			if(tileSet.contains(key))
				return tileSet.getTile(key);
		}

		return null;
	}

	public void drawTile(KEY key, Graphics g, Rectangle bounds, Component imageObserver)
	{
		if(key == null) return;
		if(g == null) return;
		if(bounds == null) return;

		BufferedImage tileImage = tiles.get(key);
		if(tileImage != null)
		{
			g.drawImage(
				tileImage,
				(int)bounds.getMinX(),				// dest left
				(int)bounds.getMinY(),				// dest top
				(int)bounds.getMaxX(),				// dest right
				(int)bounds.getMaxY(),				// dest bottom
				0,									// src left
				0,									// src top
				tileImage.getWidth(),				// src right
				tileImage.getHeight(),				// src bottom
				imageObserver						// to notify of image updates
			);
//			g.drawImage(tileImage, bounds.x, bounds.y, imageObserver);
			return;
		}

		for(TileSet<KEY> tileSet : tileSets)
		{
			if(tileSet.contains(key))
			{
				tileSet.drawTile(key, g, bounds, imageObserver);
				return;
			}
		}
	}
}